1 /*
2  * Lazy Load - jQuery plugin
for lazy loading images
3  *
4  * Copyright (c)
2007-2012 Mika Tuupola
5  *
6  * Licensed under the MIT license:
7  * http://www.opensource.org/licenses/mit-license.php
8  *
9  * Project home:
10  * http://www.appelsiini.net/projects/lazyload
11  *
12  * Version:
1.8.0
13  *
14  */

15 (function($, window) {
16     
var $window = $(window);
17
18     $.fn.lazyload = function(options) {
19         
var elements = this;
20         
var $container;
21         
var settings = {
22             threshold :
0,
23             failure_limit :
0,
24             
event : "scroll",
25             effect :
"show",
26             container : window,
27             data_attribute :
"original",
28             skip_invisible :
true,
29             appear :
null,
30             load :
null
31         };
32
33         function update() {
34             
var counter = 0;
35       
36             elements.each(function() {
37                 
var $this = $(this);
38                 
if (settings.skip_invisible && !$this.is(":visible")) {
39                     
return;
40                 }
41                 
if ($.abovethetop(this, settings) ||
42                     $.leftofbegin(
this, settings)) {
43                         
/* Nothing. */
44                 }
else if (!$.belowthefold(this, settings) &&
45                     !$.rightoffold(
this, settings)) {
46                         $
this.trigger("appear");
47                 }
else {
48                     
if (++counter > settings.failure_limit) {
49                         
return false;
50                     }
51                 }
52             });
53
54         }
55
56         
if(options) {
57             
/* Maintain BC for a couple of versions. */
58             
if (undefined !== options.failurelimit) {
59                 options.failure_limit = options.failurelimit;
60                 delete options.failurelimit;
61             }
62             
if (undefined !== options.effectspeed) {
63                 options.effect_speed = options.effectspeed;
64                 delete options.effectspeed;
65             }
66
67             $.extend(settings, options);
68         }
69
70         
/* Cache container as jQuery as object. */
71         $container = (settings.container === undefined ||
72                       settings.container === window) ? $window : $(settings.container);
73
74         
/* Fire one scroll event per scroll. Not one scroll event per image. */
75         
if (0 === settings.event.indexOf("scroll")) {
76             $container.bind(settings.
event, function(event) {
77                 
return update();
78             });
79         }
80
81         
this.each(function() {
82             
var self = this;
83             
var $self = $(self);
84
85             self.loaded =
false;
86
87             
/* When appear is triggered load original image. */
88             $self.one(
"appear", function() {
89                 
if (!this.loaded) {
90                     
if (settings.appear) {
91                         
var elements_left = elements.length;
92                         settings.appear.call(self, elements_left, settings);
93                     }
94                     $(
"<img />")
95                         .bind(
"load", function() {
96                             $self
97                                 .hide()
98                                 .attr(
"src", $self.data(settings.data_attribute))
99                                 [settings.effect](settings.effect_speed);
100                             self.loaded =
true;
101
102                             
/* Remove image from array so it is not looped next time. */
103                             
var temp = $.grep(elements, function(element) {
104                                 
return !element.loaded;
105                             });
106                             elements = $(temp);
107
108                             
if (settings.load) {
109                                 
var elements_left = elements.length;
110                                 settings.load.call(self, elements_left, settings);
111                             }
112                         })
113                         .attr(
"src", $self.data(settings.data_attribute));
114                 }
115             });
116
117             
/* When wanted event is triggered load original image */
118             
/* by triggering appear. */
119             
if (0 !== settings.event.indexOf("scroll")) {
120                 $self.bind(settings.
event, function(event) {
121                     
if (!self.loaded) {
122                         $self.trigger(
"appear");
123                     }
124                 });
125             }
126         });
127
128         
/* Check if something appears when window is resized. */
129         $window.bind(
"resize", function(event) {
130             update();
131         });
132
133         
/* Force initial check if images should appear. */
134         update();
135         
136         
return this;
137     };
138
139     
/* Convenience methods in jQuery namespace. */
140     
/* Use as $.belowthefold(element, {threshold : 100, container : window}) */
141
142     $.belowthefold = function(element, settings) {
143         
var fold;
144         
145         
if (settings.container === undefined || settings.container === window) {
146             fold = $window.height() + $window.scrollTop();
147         }
else {
148             fold = $(settings.container).offset().top + $(settings.container).height();
149         }
150
151         
return fold <= $(element).offset().top - settings.threshold;
152     };
153     
154     $.rightoffold = function(element, settings) {
155         
var fold;
156
157         
if (settings.container === undefined || settings.container === window) {
158             fold = $window.width() + $window.scrollLeft();
159         }
else {
160             fold = $(settings.container).offset().left + $(settings.container).width();
161         }
162
163         
return fold <= $(element).offset().left - settings.threshold;
164     };
165         
166     $.abovethetop = function(element, settings) {
167         
var fold;
168         
169         
if (settings.container === undefined || settings.container === window) {
170             fold = $window.scrollTop();
171         }
else {
172             fold = $(settings.container).offset().top;
173         }
174
175         
return fold >= $(element).offset().top + settings.threshold + $(element).height();
176     };
177     
178     $.leftofbegin = function(element, settings) {
179         
var fold;
180         
181         
if (settings.container === undefined || settings.container === window) {
182             fold = $window.scrollLeft();
183         }
else {
184             fold = $(settings.container).offset().left;
185         }
186
187         
return fold >= $(element).offset().left + settings.threshold + $(element).width();
188     };
189
190     $.inviewport = function(element, settings) {
191          
return !$.rightofscreen(element, settings) && !$.leftofscreen(element, settings) &&
192                 !$.belowthefold(element, settings) && !$.abovethetop(element, settings);
193      };
194
195     
/* Custom selectors for your convenience. */
196     
/* Use as $("img:below-the-fold").something() */
197
198     $.extend($.expr[
':'], {
199         
"below-the-fold" : function(a) { return $.belowthefold(a, {threshold : 0}); },
200         
"above-the-top" : function(a) { return !$.belowthefold(a, {threshold : 0}); },
201         
"right-of-screen": function(a) { return $.rightoffold(a, {threshold : 0}); },
202         
"left-of-screen" : function(a) { return !$.rightoffold(a, {threshold : 0}); },
203         
"in-viewport" : function(a) { return !$.inviewport(a, {threshold : 0}); },
204         
/* Maintain BC for couple of versions. */
205         
"above-the-fold" : function(a) { return !$.belowthefold(a, {threshold : 0}); },
206         
"right-of-fold" : function(a) { return $.rightoffold(a, {threshold : 0}); },
207         
"left-of-fold" : function(a) { return !$.rightoffold(a, {threshold : 0}); }
208     });
209
210 })(jQuery, window);



Full source code website bán hàng thương mại điện tử gần giống shopee 469.168 lượt xem

Gõ tìm kiếm nhanh...